package com.hks360.car_treasure.fragment;

import android.content.DialogInterface;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hks360.car_treasure.R;
import com.hks360.car_treasure.common.Constant;
import com.hks360.library.nohttp.CallServer;
import com.hks360.library.nohttp.HttpCallback;
import com.hks360.library.util.UIUtil;
import com.hks360.library.widget.ILoadingDialog;
import com.umeng.analytics.MobclickAgent;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by xiaote on 2016/3/15.
 */
public abstract class BaseFragment extends Fragment implements View.OnClickListener {
    public View mRootView;

    public ImageView mRightIv;
    public TextView mTitleTv;
    public TextView mLeftTv;
    public ILoadingDialog mDialog;

    /** 初始化标题栏 */
    private void initTitleBar() {
        mLeftTv = UIUtil.findViewById(mRootView,R.id.common_titlebar_left_tv);
        mTitleTv = UIUtil.findViewById(mRootView,R.id.common_titlebar_title_tv);
        mRightIv = UIUtil.findViewById(mRootView, R.id.common_titlebar_right_iv);
    }

    /**
     * 初始化标题栏
     * @param titleResId    标题资源ID
     */
    public void initTitleBar(int titleResId) {
        initTitleBar();
        mTitleTv.setText(titleResId);
    }
    /**
     * 初始化标题栏
     * @param titleString    标题资源ID
     */
    public void initTitleBar(String titleString) {
        initTitleBar();
        mTitleTv.setText(titleString);
    }

    /**
     * 初始化标题栏
     */
    public void initTitleBar(String s,int size) {
        initTitleBar();
        mTitleTv.setText(s);
        mTitleTv.setTextSize(size);
        mTitleTv.setPadding(20,20,20,20);
    }
    /**
     * 初始化标题栏
     */
    public void initTitleBar(String s,int size,int color) {
        initTitleBar();
        mTitleTv.setText(s);
        mTitleTv.setTextSize(size);
        mTitleTv.setTextColor(color);
        mTitleTv.setPadding(20,20,20,20);
    }
    /**
     * 初始化带刷新按钮的标题栏
     * @param titleResId    标题资源ID
     */
    public void initTitleBarOfRefresh(int leftResId,int titleResId) {
        initTitleBar();
        mLeftTv.setVisibility(View.VISIBLE);
        mRightIv.setVisibility(View.VISIBLE);
        mLeftTv.setText(leftResId);
        mTitleTv.setText(titleResId);
        mLeftTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        mLeftTv.setCompoundDrawablePadding(0);
    }
    public void initDialog() {
        mDialog = new ILoadingDialog.Builder(getContext()).create();
        mDialog.setCancelable(Constant.LOADING_DIALOG_CANCEL);
//        mDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
//            @Override
//            public void onCancel(DialogInterface dialog) {
//                mDialog.hide();
////                CallServer.getRequestInstance().cancelAll();
//            }
//        });
    }

    public void initDialog(int txtResId) {
        mDialog = new ILoadingDialog.Builder(getContext()).setMessage(txtResId).create();
        mDialog.setCancelable(Constant.LOADING_DIALOG_CANCEL);
        mDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                CallServer.getRequestInstance().cancelAll();
            }
        });
    }

    /** 初始化控件 */
    public abstract void setupView();
    /** 初始化数据 */
    public abstract void initData();
    /** 添加控件监听器 */
    public abstract void addListener();
    /** 初始化界面 */
    public abstract void initView();
    /** 点击事件触发回调方法 */
    public abstract void viewClick(View v);

    @Override
    public void onClick(View v) {
        viewClick(v);
    }
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("MainScreen"); //统计页面，"MainScreen"为页面名称，可自定义
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("MainScreen");
    }
}
